<template>
  <el-row>
    <el-col :span="24">
      <el-card>
        <el-form ref="searchForm" class="el-form-col search-form" :model="search">
          <el-row :gutter="15">
            <el-col :lg="4" :md="6" v-if="state == 1">
              <el-form-item prop="gainorlossSource">
                <el-select v-model="search.gainorlossSource" placeholder="请选择报亏来源">
                  <el-option value="1" label="手动创建"></el-option>
                  <el-option value="2" label="盘库"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item prop="founderUserName">
                <el-input v-model="search.founderUserName" placeholder="创建人"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6" v-if="state == 0">
              <el-form-item prop="foundStartDate">
                <el-date-picker type="date"
                                placeholder="创建时间:开始"
                                v-model="search.foundStartDate"
                                value-format="yyyy-MM-dd"
                                style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6" v-if="state == 0">
              <el-form-item prop="foundEndDate">
                <el-date-picker type="date"
                                placeholder="创建时间:结束"
                                v-model="search.foundEndDate"
                                value-format="yyyy-MM-dd"
                                style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6" v-if="state == 1">
              <el-form-item prop="auditorName">
                <el-input v-model="search.auditorName" placeholder="审核人"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit" @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <el-col :span="12">
                    <el-button @click="resetFields('searchForm')" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </el-col>
    <el-col :span="24">
      <BasicTable
        ref="BasicTable"
        url="/ms-warehouse-order/inventory/findGainOrLossList"
        :tabs="tabs"
        :initParams="{gainorlossState: 2}"
        :columns="columns">
        <template slot="column">
          <el-table-column fixed="right" width="100px" align="center" label="操作" v-if="state == 0">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="showDetail(scope.row, state)"
              >
                审核
              </el-button>
            </template>
          </el-table-column>
          <el-table-column fixed="right" width="100px" align="center" label="操作" v-if="state == 1">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="showDetail(scope.row, state)"
              >
                详情
              </el-button>
            </template>
          </el-table-column>
        </template>
      </BasicTable>
    </el-col>
  </el-row>
</template>
<script>
// import {filterDataBaseDictionary, unixTimestampFilter} from '@/filters'
import {unixTimestampFilter} from '@/filters'
// import {copy, dotData} from '@/utils'
import {copy} from '@/utils'
// import {getAllWarehouse} from '@/utils/selectOptions'
export default {
  name: 'lossReportIndex',
  data() {
    const that = this
    let columns = [
      {
        label: '报亏单编号',
        data: 'gainorlossNo'
      },
      {
        label: '报亏来源',
        data: 'gainorlossSource',
        render(data, full) {
          if (data === 1) {
            return '手动创建'
          } else if (data === 2) {
            return '盘库'
          } else {
            return '未知'
          }
        }
      },
      {
        label: '报亏包件数据量',
        data: 'totalGainorlossNum'
      },
      {
        label: '创建人',
        data: 'founderUserName'
      },
      {
        label: '创建日期',
        data: 'foundtime',
        render(data, full) {
          return unixTimestampFilter(data)
        }
      }
    ]
    let secondColumns = copy(columns, true)
    secondColumns.splice(3, 0, {
      label: '通过审核总数量',
      data: 'totalAuditNum'
    }, {
      label: '审核备注',
      data: 'auditMemo'
    })
    secondColumns.splice(secondColumns.length - 1, 0, {
      label: '审核人',
      data: 'auditorName'
    }, {
      label: '审核时间',
      width: '134px',
      data: 'auditTime',
      render(data, full) {
        return unixTimestampFilter(data)
      }
    })
    return {
      state: 0,
      searchLoading: false,
      search: {
        gainorlossSource: '',
        founderUserName: '',
        foundStartDate: '',
        foundEndDate: '',
        auditorName: ''
      },
      columns,
      tabs: [
        {
          label: '待审核',
          callback: (params) => {
            params.auditState = '0'
            that.state = '0'
          }
        },
        {
          label: '已审核',
          callback: (params) => {
            params.auditState = '1'
            that.state = '1'
          },
          columns: secondColumns
        }
      ],
      typeArr: [],
      item: {}
    }
  },
  created() {
  },
  methods: {
    // 搜索
    searchFunc(search) {
      const basicTable = this.$refs.BasicTable
      if (basicTable) {
        let params = copy(search)
        this.searchLoading = true
        basicTable.search(params).finally(_ => {
          this.searchLoading = false
        })
      }
    },
    // 重置表单
    resetFields() {
      const searchForm = this.$refs.searchForm
      if (searchForm) {
        searchForm.resetFields()
      }
    },
    // 表格刷新
    refresh() {
      this.searchFunc(this.search)
    },
    // 跳转详情
    showDetail(data, state) {
      this.$router.push({path: '/ss-oms/warehouse/loss-report/show/' + data.id + '/?state=' + state})
    }
  }
}
</script>
